<template >
    <div class="params">
        <!-- 面包屑 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/menu/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item><a href="/menu/">商品管理</a></el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片 -->
        <el-card class="box-card">
            <!-- 提示 -->
            <el-alert title="注意：只允许为第三级分类设置相关参数！" type="warning" show-icon>
            </el-alert>
            <!-- 级联选择器 -->
            选择商品分类 :
            <el-cascader v-model="value" :options="options"
                :props="{ expandTrigger: 'hover' ,label:'cat_name',value:'cat_id',children:'children' }"
                @change="handleChange">
            </el-cascader>
            <!-- tabs切换 -->
            <el-tabs v-model="activeName" @tab-click="handleClick">
                <el-tab-pane label="动态参数" name="many">
                    <!-- 按钮 -->
                    <el-button type="primary" :disabled="isedit" size="small" @click="dialogFormVisible=true">添加参数
                    </el-button>
                    <!-- 表格 -->
                    <el-table :data="tableData" style="width: 100%" border>
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <!-- tag   -->
                                <el-tag v-for="item,index in scope.row.attr_vals" :key="index" closable
                                    :disable-transitions="false" @close="handleClose(index,scope.row)">
                                    {{item}}
                                </el-tag>
                                <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue"
                                    ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(scope.row)"
                                    @blur="handleInputConfirm(scope.row)">
                                </el-input>
                                <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag
                                </el-button>

                            </template>
                        </el-table-column>
                        <el-table-column label="" type="index">
                        </el-table-column>
                        <el-table-column label="参数名称" prop="attr_name">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" @click=update(scope.row) icon="el-icon-edit" size="mini">编辑
                                </el-button>
                                <el-button type="danger" @click=del(scope.row) icon="el-icon-delete" size="mini">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
                <el-tab-pane label="静态属性" name="only">
                    <!-- 按钮 -->
                    <el-button type="primary" :disabled="edit" size="small" @click="dialogFormVisible1=true">添加属性
                    </el-button>
                    <!-- 表格 -->
                    <el-table :data="tableData1" style="width: 100%" border>
                        <el-table-column type="expand">
                            <template slot-scope="scope">
                                <!-- tag   -->
                                <el-tag v-for="item,index in scope.row.attr_vals" :key="index" closable
                                    :disable-transitions="false" @close="handleClose(index,scope.row)">
                                    {{item}}
                                </el-tag>
                                <el-input class="input-new-tag" v-if="inputVisible" v-model="inputValue"
                                    ref="saveTagInput" size="small" @keyup.enter.native="handleInputConfirm(scope.row)"
                                    @blur="handleInputConfirm(scope.row)">
                                </el-input>
                                <el-button v-else class="button-new-tag" size="small" @click="showInput">+ New Tag
                                </el-button>
                            </template>
                        </el-table-column>
                        <el-table-column label="" type="index">
                        </el-table-column>
                        <el-table-column label="属性名称" prop="attr_name">
                        </el-table-column>
                        <el-table-column label="操作">
                            <template slot-scope="scope">
                                <el-button type="primary" @click=updat(scope.row) icon="el-icon-edit" size="mini">编辑
                                </el-button>
                                <el-button type="danger" @click=del(scope.row) icon="el-icon-delete" size="mini">删除
                                </el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                </el-tab-pane>
            </el-tabs>
        </el-card>
        <!--添加动态参数 -->
        <el-dialog title="添加动态参数" :visible.sync="dialogFormVisible">
            <el-form :model="form">
                <el-form-item label="动态参数名" label-width="100px">
                    <el-input v-model="form.attr_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </div>
        </el-dialog>
        <!--添加静态属性 -->
        <el-dialog title="添加静态属性" :visible.sync="dialogFormVisible1" label-width="60px">
            <el-form :model="form">
                <el-form-item label="静态属性名">
                    <el-input v-model="form.attr_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="add()">确 定</el-button>
            </div>
        </el-dialog>
        <!--编辑动态参数 -->
        <el-dialog title="编辑动态参数" :visible.sync="dialogFormVisible3">
            <el-form :model="form">
                <el-form-item label="动态参数名" label-width="90px">
                    <el-input v-model="form.attr_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible3 = false">取 消</el-button>
                <el-button type="primary" @click="do_update()">确 定</el-button>
            </div>
        </el-dialog>
        <!--编辑静态属性 -->
        <el-dialog title="编辑静态属性" :visible.sync="dialogFormVisible4">
            <el-form :model="form">
                <el-form-item label="静态属性名" label-width="90px">
                    <el-input v-model="form.attr_name" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible4 = false">取 消</el-button>
                <el-button type="primary" @click="do_updat()">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>
export default {
    name: 'params',
    data() {
        return {
            dialogFormVisible: false,
            dialogFormVisible1: false,
            dialogFormVisible3: false,
            dialogFormVisible4: false,
            form: { attr_name: '' },
            value: [],
            options: [],
            id: 0,
            attrvals: [],
            activeName: "many",
            isedit: true,
            edit: true,
            tableData: [],
            tableData1: [],
            dynamicTags: [],
            inputVisible: false,
            inputValue: ''
        }
    },
    mounted() {
        this.getcascader()
        this.getparlist()
    },
    methods: {
        // 挂载分类
        async getcascader() {
            var res = await this.axios({
                url: '/v1/categories',
            })
            console.log(res);
            this.options = res.data.data
        },
        // 挂载params
        async getparlist() {
            if (this.value.length == 3) {
                let id = this.value[2]
                this.isedit = false
                this.edit = false
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes`,
                    params: {
                        sel: this.activeName
                    }
                })
                console.log(res);
                if (this.activeName == 'many') {
                    this.tableData = res.data.data
                    this.tableData.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                } else if (this.activeName == 'only') {
                    this.tableData1 = res.data.data
                    this.tableData1.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                }

            }
        },
        // 级联选择器发送改变时触发
        async handleChange() {
            if (this.value.length == 3) {
                let id = this.value[2]
                this.isedit = false
                this.edit = false
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes`,
                    params: {
                        sel: this.activeName
                    }
                })
                console.log(res);
                if (this.activeName == 'many') {
                    this.tableData = res.data.data
                    this.tableData.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                } else if (this.activeName == 'only') {
                    this.tableData1 = res.data.data
                    this.tableData1.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                }


            }

        },
        // tab切换时触发
        async handleClick() {
            if (this.value.length == 3) {
                let id = this.value[2]
                this.isedit = false
                this.edit = false
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes`,
                    params: {
                        sel: this.activeName
                    }
                })
                console.log(res);
                if (this.activeName == 'many') {
                    this.tableData = res.data.data
                    this.tableData.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                } else if (this.activeName == 'only') {
                    this.tableData1 = res.data.data
                    this.tableData1.forEach(item => {
                        item.attr_vals = item.attr_vals.split(' ')
                        if (item.attr_vals[0] == '') {
                            item.attr_vals = []
                        }
                    })
                }
            }
        },
        // 删除tag
        async handleClose(index, row) {

            let id = this.value[2]
            let attrid = row.attr_id
            let attrname = row.attr_name
            row.attr_vals.splice(index, 1)
            var res = await this.axios({
                url: `/v1/categories/${id}/attributes/${attrid}`,
                method: "PUT",
                data: {
                    attr_name: attrname,
                    attr_sel: this.activeName,
                    attr_vals: row.attr_vals.join(' ')
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });

            }


        },
        //    显示input
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        //    添加tag
        async handleInputConfirm(row) {
            if (this.inputValue == '') {
                return this.$message.error('tag不能为空')
            }
            let id = this.value[2]
            let attrid = row.attr_id
            let attrname = row.attr_name
            let kw = this.inputValue
            let arr = row.attr_vals
            console.log(arr, 'arr');
            arr.push(kw)
            var res = await this.axios({
                url: `/v1/categories/${id}/attributes/${attrid}`,
                method: "PUT",
                data: {
                    attr_name: attrname,
                    attr_sel: this.activeName,
                    attr_vals: row.attr_vals.join(' ')
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message({
                    type: 'success',
                    message: '添加成功!'
                });
                this.inputValue = ''
                this.inputVisible = false;
            }
        },
        // 添加动态参数
        async add() {
            this.form.attr_name = ''
            if (this.form.attr_name == '') {
                this.$message.error('动态参数不能为空')
            } else { 
                let id = this.value[2]
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes`,
                    method: 'POST',
                    data: {
                        attr_name: this.form.attr_name,
                        attr_sel: this.activeName,
                    }
                })
                console.log(res);
                if (res.data.meta.status == 201) {
                    this.$message.success(res.data.meta.msg)
                    this.getparlist()
                    this.form.attr_name = ''
                    this.dialogFormVisible = false
                }
            }

        },
        // 添加静态参数
        async add() {
            this.form.attr_name = ''
            if (this.form.attr_name == '') {
                this.$message.error('动态参数不能为空')
            } else {
                let id = this.value[2]
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes`,
                    method: 'POST',
                    data: {
                        attr_name: this.form.attr_name,
                        attr_sel: this.activeName,
                    }
                })
                console.log(res);
                if (res.data.meta.status == 201) {
                    this.$message.success(res.data.meta.msg)
                    this.getparlist()
                    this.form.attr_name = ''
                    this.dialogFormVisible1 = false
                }
            }

        },
        // 编辑动态参数
        update(row) {
            this.dialogFormVisible3 = true,
                this.id = row.attr_id
            this.attrvals = row.attr_vals
            this.form.attr_name = row.attr_name 
        },
        async do_update() {
            let attrid = this.id
            let id = this.value[2]
            let attrname = this.form.attr_name
            let attrvals = this.attrvals
            var res = await this.axios({
                url: `/v1/categories/${id}/attributes/${attrid}`,
                method: "PUT",
                data: {
                    attr_name: attrname,
                    attr_sel: this.activeName,
                    attr_vals: attrvals.join(' ')
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message({
                    type: 'success',
                    message: '编辑成功!'
                });
                this.getparlist()
                this.form.attr_name = ''
                this.dialogFormVisible3 = false
            }
        },
        // 编辑静态属性
        updat(row) {
            this.dialogFormVisible4 = true,
                this.id = row.attr_id
            this.attrvals = row.attr_vals
            this.form.attr_name = row.attr_name
        },
        async do_updat() {
            let attrid = this.id
            let id = this.value[2]
            let attrname = this.form.attr_name
            let attrvals = this.attrvals
            var res = await this.axios({
                url: `/v1/categories/${id}/attributes/${attrid}`,
                method: "PUT",
                data: {
                    attr_name: attrname,
                    attr_sel: this.activeName,
                    attr_vals: attrvals.join(' ')
                }
            })
            console.log(res);
            if (res.data.meta.status == 200) {
                this.$message({
                    type: 'success',
                    message: '编辑成功!'
                });
                this.getparlist()
                this.form.attr_name = ''
                this.dialogFormVisible4 = false
            }
        },
        // 删除动态参数与静态数据
        del(row) {
            this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(async () => {
                let id = this.value[2]
                let attrid = row.attr_id
                var res = await this.axios({
                    url: `/v1/categories/${id}/attributes/${attrid}`,
                    method: 'DELETE',
                })
                this.$message({
                    type: 'success',
                    message: '删除成功!'
                });
                this.getparlist()
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });

        }
    }
}
</script>
<style >
.box-card {
    width: 100%;
    margin-top: 15px;
}

.el-cascader {
    margin: 20px 0;
    width: 50%;
}

.el-tag+.el-tag {
    margin-left: 10px;
}

.button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
}

.input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
}
</style>
